package com.doudou.base.resources.controller;

import com.doudou.base.resources.entity.dto.user.account.UserAccountInfoAddDto;
import com.doudou.base.resources.entity.dto.user.account.UserAccountInfoUpdateDto;
import com.doudou.base.resources.service.UserAccountInfoService;
import com.doudou.client.entity.UserAccountInfoDto;
import com.doudou.client.entity.UserDetailsInfoDto;
import com.doudou.commons.entity.PageInfo;
import com.doudou.commons.entity.PaginationSupport;
import com.doudou.commons.entity.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户账号的详情  前端控制器
 * @author 傻男人
 * @date 2020-05-14
 */
@Slf4j
@RestController
@RequestMapping("/base/user")
@Api(value = "用户账号的详情信息",tags = "用户账号的详情信息")
public class UserAccountInfoController {

    @Autowired
    private UserAccountInfoService userAccountInfoService;

    /**
     * 添加用户账号的详情
     * @param userAccountInfoAddDto
     * @param result
     * @return
     */
    @ApiOperation(value = "添加用户账号的详情",notes = "添加用户账号的详情")
    @PostMapping(value = "/account")
    public Result addUserAccountInfo(@Valid @RequestBody @ApiParam(value = "用户账号的详情添加dto",required = true) UserAccountInfoAddDto userAccountInfoAddDto, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        UserAccountInfoDto userAccountInfoDto = userAccountInfoService.addUserAccountInfo(userAccountInfoAddDto);
        return Result.ok(userAccountInfoDto);
    }

    /**
     * 根据ids删除用户账号的详情
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据ids删除用户账号的详情",notes = "根据ids删除用户账号的详情")
    @ApiImplicitParam(name = "ids",value="用户账号的详情ids",dataType="String",required = true,paramType = "path")
    @DeleteMapping(value = "/account/{ids}")
    public Result deleteUserAccountInfo(@PathVariable("ids") String ids) {
        Integer number = userAccountInfoService.deleteUserAccountInfo(ids);
        return Result.ok(number);
    }

    /**
     * 根据id修改用户账号的详情
     * @param id
     * @param userAccountInfoUpdateDto
     * @param result
     * @return
     */
    @ApiOperation(value = "根据id修改用户账号的详情",notes = "修改用户账号的详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value="用户账号的详情id",dataType="Long",required = true,paramType = "path")})
    @PutMapping(value = "/account/{id}")
    public Result updateUserAccountInfo(@PathVariable("id")  Long id,@Valid @RequestBody @ApiParam(value = "用户账号的详情修改dto",required = true) UserAccountInfoUpdateDto userAccountInfoUpdateDto, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        userAccountInfoUpdateDto.setId(id);
        Integer sum = userAccountInfoService.updateUserAccountInfo(userAccountInfoUpdateDto);
        return Result.ok(sum);
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @ApiOperation(value = "用户登录",notes = "用户登录")
    @ApiImplicitParams({@ApiImplicitParam(name = "username",value="用户名",dataType="String",required = true),
            @ApiImplicitParam(name = "password",value="密码",dataType="String",required = true)})
    @GetMapping(value = "/account/login")
    public Result findByUsernameAndPassword(@RequestParam("username")  String username,
                                      @RequestParam("password")  String password) {
        UserDetailsInfoDto userDetailsInfoDto = userAccountInfoService.findByUsernameAndPassword(username,password);
        return Result.ok(userDetailsInfoDto);
    }

    /**
     * 分页查询用户
     * @param pageInfo  分页信息
     * @return
     */
    @ApiOperation(value = "分页查询用户",notes = "分页查询用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageInfo",value="分页信息",dataType="PageInfo",required = true)})
    @GetMapping(value = "/account/page")
    public Result findPageHelper(PageInfo pageInfo) {
        PaginationSupport<UserAccountInfoDto> result = userAccountInfoService.findPageHelper(pageInfo);
        return Result.ok(result);
    }

}
